
from exts import db

from apps.models.base import BaseModel
from werkzeug.security import generate_password_hash, check_password_hash

# class Login:
#     # 验证状态
#     # TURE 以验证
#     # FALSE 为验证 默认值为FALSE
#     EMAILTURE = 1
#     EMAILFALSE = 0


# class User(db.Model):
#     __tablename__ = 'user'  # 表名
#     # 用户id
#     id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True)
#     # 用户名
#     username = db.Column(db.String(20), nullable=False)
#     # 用户密码
#     password = db.Column(db.String(256), nullable=False)
#     # 用户手机号码
#     phone = db.Column(db.String(20), nullable=False)
#     # 用户邮箱
#     email = db.Column(db.String(22), nullable=False)
#     # 是否验证了用户
#     email_over = db.Column(db.Integer, default=Login.EMAILFALSE, )  # 邮箱是否验证，默认值false
#     # 创建时间自动添加
#     add_time = db.Column(db.DateTime, default=datetime.datetime.now())
#     # 合约关系
#     ags = db.relationship('Agreement', backref='user')
#
#     def __str__(self):
#         return self.username


class User(BaseModel):
    username = db.Column(db.String(20), nullable=False, unique=True)
    phone = db.Column(db.String(11), unique=True)
    email = db.Column(db.String(50), nullable=False, unique=True)
    __password = db.Column(db.String(256), nullable=False)

    # 合约关系
    ags = db.relationship('Agreement', secondary='pour',backref=db.backref('user_agree',lazy='dynamic'),lazy='dynamic')

    @property
    def password(self):
        return Exception("password can't get!")

    # print(user.password)

    @password.setter
    def password(self, password):
        self.__password = generate_password_hash(password)

    # user.password='aaaaaa'

    def check_password(self, password):
        return check_password_hash(self.__password, password)

    def __str__(self):
        return self.username

